<?php
class User {
    public function show_alert($alert){
        if(isset($alert)){
            $types = array('success', 'warning', 'danger');
            if(strlen($alert[0]) > 0 && in_array($alert[1], $types)){
                echo '<p class="bg-'.$alert[1].' text-center">'.$alert[0].'</p>';
            }
        }
    }

    public function is_logged(){
        if(isset($_SESSION['uid']) && is_numeric($_SESSION['uid'])) return 1;
        else return 0;
    }

    public function is_leader(){
        if($this->is_logged()){
            $database = Database::getInstance();
            $leader_query = $database -> dbh -> prepare("SELECT leader FROM student WHERE id = '".$_SESSION['uid']."' LIMIT 1");
            if($leader_query->execute()) {
                $leader = $leader_query -> fetchColumn();
                if($leader == '1') return 1;
            }
            else return 0;
        }
    }

    public function is_admin(){
        if($this->is_logged()){
            $database = Database::getInstance();
            $leader_query = $database -> dbh -> prepare("SELECT leader FROM student WHERE id = '".$_SESSION['uid']."' LIMIT 1");
            if($leader_query->execute()) {
                $leader = $leader_query -> fetchColumn();
                if($leader == '2') return 1;
            }
            else return 0;
        }
    }

    public function check_login($login) // sprawdza, czy dany login jest wolny
    {
		$database = Database::getInstance();
		$check_query = $database->dbh->prepare("SELECT login FROM student WHERE login='{$login}'");
		$check_query->execute();
		if($check_query->rowCount() > 0){ 
			return 0; // znaleziono w bazie użytklownika z danym loginem
		}else{
			return 1; //jest wolny
		}
    }
	
	public function register_user($login,$password,$firstName,$lastName,$album,$mail,$wydzial,$kierunek,$year) // rejestruje uzytkownika
    {
		$database = Database::getInstance();
		$register_query = $database->dbh->prepare("
		    INSERT INTO `student` (login, password, firstName, lastName, album, mail, department, direction, year)
		    VALUES ('$login', '$password', '$firstName', '$lastName', '$album', '$mail', '$wydzial', '$kierunek', '$year')
		    ");

		if($register_query->execute()){
			return 1; // zarejestrowano
		}else{
			return 0; // coś się nie udało
		}
    }
	
	public function get_id() // zwraca id uzytkownika
    {
		$database = Database::getInstance();
		$login = $_SESSION['login'];
		$register_query = $database->dbh->prepare("SELECT id FROM student WHERE login = '$login'");
		
		if($register_query->execute()){ 
			$id = $register_query->fetchColumn();
			return $id; // znaleziono id
		}else{
			return 0; // nie znaleziono
		}
    }

    public function get_user() // zwraca imię i nazwisko uzytkownika
    {
        $database = Database::getInstance();
        $login = $_SESSION['login'];
        $register_query = $database->dbh->prepare("SELECT firstName, lastName, department, direction, year FROM student WHERE login = '$login'");

        if($register_query->execute()){
            $student = $register_query->fetchAll();
            return $student; // znaleziono id
        }else{
            return 0; // nie znaleziono
        }
    }

    public function find_leader($department, $direction, $year) // zwraca id swojego starosty
    {
        $database = Database::getInstance();
        $register_query = $database->dbh->prepare("
            SELECT id
            FROM student
            WHERE department = '$department' AND direction = '$direction' AND year = '$year' AND leader='1'
            LIMIT 1
            ");

        if($register_query->execute()){
            $leader = $register_query->fetchColumn();
            return $leader; // znaleziono id
        }else{
            return 0; // nie znaleziono
        }
    }

    public function get_students()
    {
        $database = Database::getInstance();
        $student_pattern = $this->get_user(); //pobierz studentów o pokrywających się danych w tabeli

        $department = $student_pattern[0]['department'];
        $direction = $student_pattern[0]['direction'];
        $year = $student_pattern[0]['year'];

        $register_query = $database->dbh->prepare("
            SELECT firstName, lastName, album, mail
            FROM student
            WHERE department = '$department' AND direction = '$direction' AND year = '$year'
            ");
        $register_query->execute();
        $result = $register_query->fetchAll();

        foreach($result as $id => $student) {
            $students[$id]['firstName'] = $student['firstName'];
            $students[$id]['lastName'] = $student['lastName'];
            $students[$id]['album'] = $student['album'];
            $students[$id]['mail'] = $student['mail'];
        }
        return $students;
    }

} 

?>